package com.ruoyi.source.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.source.api.domain.SrcTask;
import com.ruoyi.source.api.domain.SrcWorkRecord;
import com.ruoyi.source.service.ISrcTaskService;
import com.ruoyi.source.service.ISrcWorkRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 工作记录Controller
 * 
 * @author ruoyi
 * @date 2024-07-23
 */
@RestController
@RequestMapping("/work/record")
public class SrcWorkRecordController extends BaseController {
    @Autowired
    private ISrcWorkRecordService srcWorkRecordService;

    /**
     * 查询工作记录列表
     */
    @RequiresPermissions("source:record:list")
    @GetMapping("/list")
    public TableDataInfo list(SrcWorkRecord srcWorkRecord)
    {
        startPage();
        List<SrcWorkRecord> list = srcWorkRecordService.selectSrcWorkRecordList(srcWorkRecord);
        return getDataTable(list);
    }

    /**
     * 导出工作记录列表
     */
    @RequiresPermissions("source:record:export")
    @Log(title = "工作记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SrcWorkRecord srcWorkRecord)
    {
        List<SrcWorkRecord> list = srcWorkRecordService.selectSrcWorkRecordList(srcWorkRecord);
        ExcelUtil<SrcWorkRecord> util = new ExcelUtil<SrcWorkRecord>(SrcWorkRecord.class);
        util.exportExcel(response, list, "工作记录数据");
    }

    /**
     * 获取工作记录详细信息
     */
    @RequiresPermissions("source:record:query")
    @GetMapping(value = "/{workId}")
    public AjaxResult getInfo(@PathVariable("workId") Long workId)
    {
        return success(srcWorkRecordService.selectSrcWorkRecordByWorkId(workId));
    }

    /**
     * 新增工作记录
     */
    @Autowired
    private ISrcTaskService srcTaskService;
    @RequiresPermissions("source:record:add")
    @Log(title = "工作记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SrcWorkRecord srcWorkRecord) {
        boolean save = srcWorkRecordService.save(srcWorkRecord);
        if (save) {
            SrcTask srcTask = srcTaskService.getById(srcWorkRecord.getWorkId());
            if (srcTask != null) {
                if (srcTask.getTaskId() == 0)
                    srcTask.setStatus(String.valueOf(1));
            }
        }
        return toAjax(true);
    }

    /**
     * 修改工作记录
     */
    @RequiresPermissions("source:record:edit")
    @Log(title = "工作记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SrcWorkRecord srcWorkRecord)
    {
        return toAjax(srcWorkRecordService.updateSrcWorkRecord(srcWorkRecord));
    }

    /**
     * 删除工作记录
     */
    @RequiresPermissions("source:record:remove")
    @Log(title = "工作记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{workIds}")
    public AjaxResult remove(@PathVariable Long[] workIds)
    {
        return toAjax(srcWorkRecordService.deleteSrcWorkRecordByWorkIds(workIds));
    }
}
